var addClass;
require(['monitor'], function () {
	
	var selectCarId="" ;	
	var _currentBoxNo = 0;//当前电池BMS序号，为0时代表该车不为双路BMS
    $('#monitor').monitor({
        canId: '11',
        getOtherCommand:function(){
        	if ($('#bms_no_list').is(':visible')) {//bms radio可以选择时说明是双路BMS，下发多了个参数boxNo
    			if ($('#bms_no1').is(':checked')) {
    				_currentBoxNo = 1;
    			} else {
    				_currentBoxNo = 2;
    			}
    			return 'btryNo:"' + _currentBoxNo + '"';
    		} else {
    			return '';
    		}
        },
        callback: function (data) {
        	loadData(data);
        },
        clearData: function () {
        	loadData({});
            loadSingleBatteryData({})
        }
    });
    
	//左侧树
	commonFun.leftTreeNodeClickObj['carChange'] = function(event,
			treeId, treeNode) {
		if($('#monitor').monitor('getStatus')=='monitor'){
			return;
		}
		selectCarId=treeNode.id;
		_currentBoxNo = 0;
		$('#bms_no_list')[treeNode.typeDetail === "CAT00007" ? 'show' : 'hide']();
	};
    
	function loadData(data) {
        $("#batteryClass").setViewData(data, 'id');
        //最高电压箱号
        if((data.highestVoltageBoxNO!=undefined)&&(data.highestVoltageInboxLocation!=undefined)){
            $("#highestVoltageBoxNO").html(data.highestVoltageBoxNO+'/'+data.highestVoltageInboxLocation+'(块)');
        }
        else{
            $("#highestVoltageBoxNO").html("");
        }
        //最低电压箱号
        if((data.lowestVoltageBoxNO!=undefined)&&(data.lowestVoltageInboxLocation!=undefined)){
            $("#lowestVoltageBoxNO").html(data.lowestVoltageBoxNO+'/'+data.lowestVoltageInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //最高温度箱号
        if((data.highestTemperatureBoxNO!=undefined)&&(data.hightestTemperatureInboxLocation!=undefined)){
            $("#highestTemperatureBoxNO").html(data.highestTemperatureBoxNO+'/'+data.hightestTemperatureInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //最低温度箱号
        if((data.lowestTemperatureBoxNO!=undefined)&&(data.lowestTemperatureInboxLocation!=undefined)){
            $("#lowestTemperatureBoxNO").html(data.lowestTemperatureBoxNO+'/'+data.lowestTemperatureInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //电池状态
        var batteryStatus = data.batteryStatus;
        if(batteryStatus!=undefined){
            for (var i = batteryStatus.length - 1; i >= 0; i--) {
                var j = batteryStatus.length - i;
                var signStr = "否";
                var cssStr="green";
                if (batteryStatus[i] == '1') {
                    signStr = "是";
                    cssStr="red";
                }
                $("#batteryStatus" + j).removeClass();
                $("#batteryStatus" + j).addClass(cssStr);
                $("#batteryStatus" + j).html(signStr);
            }
        }
        //生成电池箱体个数列表
        createBoxTab(data.batteryBoxCount, data.batteryBoxLinkInfo);
	}
	
	//生成电池箱体个数
	function createBoxTab(boxNum, batteryBoxLinkInfo) {
        $("#btryBox tr:not(:first)").remove();
        if(boxNum!=undefined&&batteryBoxLinkInfo!=undefined){
            for (var i = boxNum - 1,j = 1; i >= 0; i--,j++) {
                var trStr = "<td class='green' id='boxStatus"+j+"'>正常</td>";
                if (batteryBoxLinkInfo[i] == '1') {
                    signStr = "<td class='red' id='boxStatus"+j+"'>异常</td>";
                }
                var $currentRow=$("#btryBox tr").eq(-1);
                //var $row = $("<tr onclick='addClass(" + j + ")' id='boxRow" + j + "'><td>第" + j + "箱</td>"+trStr+"</tr>");
                var $row = $("<tr id='" + j + "'><td>第" + j + "箱</td>"+trStr+"</tr>");
                $currentRow.after($row);
            }
        }
	}

    $("#btryBox").on('click','tr',function(){
        $("#btryBox tr").removeClass();
        var id = this.id;
        if(id){
            $(this).addClass("active");
            var command = "{canId:'14',carId:'" + selectCarId + "',timeSpan:1,interval:0,boxNo:" + id + ",btryNo:" + _currentBoxNo + "}";
            $.fn.monitor.command(command,{callback:function(data){
                loadSingleBatteryData(data);
            }});
        }
    });

    //添加一行
    function newRow(tab,trStr){
        var $currentRow=$(tab+" tr").eq(-1);
        var $row = $("<tr>"+trStr+"</tr>");
        $currentRow.after($row);
    }

    //解析单体电池
    function createVoltageTab(batteryVoltageList){
        $("#voltageTab tr:not(:first)").remove();
        if(batteryVoltageList!=undefined){
            var volArray = batteryVoltageList.split("/");
            //单体电池个数
            var volCount=volArray.length - 1;
            $("#voltageNum").html("单体电池个数：" + volCount + "个");
            var trStr = "";
            for (var i = 0; i < volCount; i++) {
                trStr += "<th>第" + (i+1) + "块</th><td>" + volArray[i] + "</td>";
                if(((i+1) % 3 == 0)){
                    newRow("#voltageTab",trStr);
                    trStr = "";
                }
                else if(i == volCount - 1){
                    for(var j=0;j<3-((i+1)%3);j++){
                        trStr += "<th></th><td></td>";
                    }
                    newRow("#voltageTab",trStr);
                    trStr = "";
                }
            }
        }
    }

    //解析温度
    function createTempTab(temperatureList){
        $("#tmprtTab tr:not(:first)").remove();
        $("#tmprtNum").html("");
        if(temperatureList!=undefined){
            var tempArray = temperatureList.split("/");
            //探头个数
            var tempCount=tempArray.length - 1;
            $("#tmprtNum").html("温度探头个数：" + tempCount + "个");
            var trStr = "";
            for (var i = 0; i < tempCount; i++) {
                trStr += "<th>探头" + (i+1) + "块</th><td>" + tempArray[i] + "</td>";
                if(((i+1) % 3 == 0)){
                    newRow("#tmprtTab",trStr);
                    trStr = "";
                }
                else if(i == tempCount - 1){
                    for(var j=0;j<3-((i+1)%3);j++){
                        trStr += "<th></th><td></td>";
                    }
                    newRow("#tmprtTab",trStr);
                    trStr = "";
                }
            }
        }
    }
	
	//单体电池箱体解析
	function loadSingleBatteryData(data) {
        createVoltageTab(data.batteryVoltageList);
        createTempTab(data.temperatureList);
	}
	
});

